package edu.chalmers;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class BookDetailActivity extends SherlockActivity{
	private BookManager bookManager = SharedBookManager.getBookManager();
	private Book currentBook;
	private int position;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		
		ActionBar actionBar = getSupportActionBar();	//Retrieve the action bar
		actionBar.setDisplayHomeAsUpEnabled(true);		//Activate up button
		actionBar.setDisplayShowTitleEnabled(false);	//Hide app title

		Intent intent = getIntent();					//Retrieve book
		position = intent.getIntExtra(Book.TITLE, 0);
		currentBook = bookManager.bookAtIndex(position);
	}
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getSupportMenuInflater();
       inflater.inflate(R.layout.detail_menu, menu);
       return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
	   int id = item.getItemId();
	   switch (item.getItemId()) {
	   	case android.R.id.home:
//	   if(id == android.R.id.home){
	   		setResult(RESULT_OK);
	   		finish();
	   		return true;
//	   } else if(id == R.id.menu_editBook){
	   	case R.id.menu_editBook:
	   		Intent intent = new Intent(this, EditActivity.class);
	   		intent.putExtra(Book.TITLE, position);
	   		startActivity(intent);
	   		return true;
//	   } else if(id == R.id.menu_removeBook){
	   	case R.id.menu_removeBook:
	   		bookManager.removeBook(currentBook);
	   		setResult(RESULT_OK);
	   		finish();
	   		return true;
//	   } else{
	   	default:
	   		return super.onOptionsItemSelected(item);
	   }
   }
   
   @Override
	public void onResume() {
		super.onResume();
		currentBook = bookManager.bookAtIndex(position);
		
		//Set text in GUI
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(currentBook.getTitle());
		TextView author = (TextView) findViewById(R.id.author);
		author.setText(currentBook.getAuthor());
		TextView course = (TextView) findViewById(R.id.course);
		course.setText(currentBook.getCourse());
		TextView isbn = (TextView) findViewById(R.id.isbn);
		isbn.setText(currentBook.getIsbn());
		TextView price = (TextView) findViewById(R.id.price);
		price.setText(currentBook.getPrice() + " SEK");
	}
}
